package com.neuedu.helloworld.notifation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.neuedu.helloworld.MainActivity;
import com.neuedu.helloworld.R;
import com.neuedu.helloworld.receiver.ConnectionBroadcastReceiver;
import com.neuedu.helloworld.service.MusicService;

public class NotificationActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnCreateNotify;
    private Button btnStartServie;
    private Button btnStopServie;
    private Button btnRegisterRecive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        btnCreateNotify = findViewById(R.id.btnCreateNotify);
        btnStartServie = findViewById(R.id.btnStartServie);
        btnStopServie = findViewById(R.id.btnStopServie);
        btnRegisterRecive = findViewById(R.id.btnRegisterRecive);


        btnCreateNotify.setOnClickListener(this);
        btnStartServie.setOnClickListener(this);
        btnStopServie.setOnClickListener(this);
        btnRegisterRecive.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.btnCreateNotify){
            createNotification();
        }else if(v.getId() == R.id.btnStartServie){
            onStartService();
        }else if(v.getId() == R.id.btnStopServie){
            onStopService();
        }else if(v.getId() == R.id.btnRegisterRecive){


            Intent intent  = new Intent();
            intent.putExtra("smg","短信的内容");
            intent.setAction("com.neuedu.SelfAction");

            //发送广播
            sendBroadcast(intent);


        }



    }

    /**
     * 停止服务  停止后台进程
     */
    private void onStopService() {
        Intent intentService  = new Intent(this, MusicService.class);
        stopService(intentService);

    }

    /**
     * 启动服务  启动后台程序
     */
    private void onStartService() {
        //显示意图

        Intent intentService  = new Intent(this, MusicService.class);
        intentService.putExtra("music","asdfasfasdfasddasdasf");

        startService(intentService);


//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
//        registerReceiver(new SystemSystemBroadcastReceiver(), intentFilter);

    }








    private void createNotification() {
        NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        //生成通知
        Intent intent = new Intent(this, MainActivity.class);


        Bundle bundle = new Bundle();
//        bundle.p
//        bundle.putString();




        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");
        intent.putExtra("id","aaa");


        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);




        Notification notification = new NotificationCompat.Builder(this, "chanleId")
                .setContentTitle("this is a notification")
                .setContentText("this is text")
                .setSmallIcon(R.drawable.add_user_25)

                //延时意图
                .setContentIntent(pi)


                .setAutoCancel(true)
                .build();


        manager.notify(1,notification);
    }


}